package com.hardware.view.controller;

import com.hardware.enums.AidierStatus;
import com.hardware.utils.MyDateUtil;
import com.hardware.view.service.TestService;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * 测试窗口
 *
 * @作者 韩宗森
 * @创建时间 2024年10月31日 10:15
 */
public class TestController {

    // 楼号
    @FXML
    private TextField floorNum;

    // 楼层
    @FXML
    private TextField floorStorey;

    // 房号
    @FXML
    private TextField roomNum;

    // 日期控件
    @FXML
    private DatePicker starDate;
    @FXML
    private TextField timeField;

    // 开始时间
    @FXML
    private TextField starTime;

    // 结束时间
    @FXML
    private TextField endTime;

    // 复制 单选框
    @FXML
    private CheckBox copyCard;

    // 卡号
    @FXML
    private TextField cardNum;


    private TestService testViewService;

    @FXML
    public void initialize() {
        // 初始化 testViewService
        testViewService = new TestService();
    }

    /**
     * 打开串口
     *
     * @param event
     */
    @FXML
    public void openPort(ActionEvent event) {
        System.out.println("打开串口 ... ");

        int ret = testViewService.openPort();
        showRespond(ret);
    }

    @FXML
    void getStarDate(ActionEvent event) {
        LocalDate selectedDate = starDate.getValue();
        String timeString = timeField.getText();

        if (selectedDate != null && !timeString.isEmpty()) {
            DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm");
            LocalTime selectedTime = LocalTime.parse(timeString, timeFormatter);

            System.out.println("Selected Date and Time: " + selectedDate + " " + selectedTime);
        } else {
            System.out.println("Please select a date and enter a valid time.");
        }
    }

    /**
     * Beep(蜂鸣器)
     *
     * @param event
     */
    @FXML
    public void beep(ActionEvent event) {
        System.out.println("Beep(蜂鸣器) ...");
        int ret = testViewService.beep();
        showRespond(ret);
    }

    /**
     * 关闭串口
     *
     * @param event
     */
    @FXML
    public void closePort(ActionEvent event) {
        System.out.println("关闭串口 ...");
        int ret = testViewService.closePort();
        showRespond(ret);
    }

    /**
     * 4.写卡
     *
     * @param event
     */
    @FXML
    public void writeCard(ActionEvent event) {
        System.out.println("写卡 ...");
        // 获取输入框的参数
        String floorNumText = floorNum.getText(); // 楼号
        String floorStoreyText = floorStorey.getText();// 楼层
        String roomNumText = roomNum.getText(); // 房号
        String starTimeText = starTime.getText(); // 开始时间
        String endTimeText = endTime.getText(); // 结束时间
        String cardNumText = cardNum.getText(); // 卡号
        boolean isCopy = copyCard.isSelected();

        System.out.println(MyDateUtil.currentTime()
                + "floorNumText: " + floorNumText
                + "; floorStoreyText: " + floorStoreyText
                + "; roomNumText: " + roomNumText
                + "; starTimeText: " + starTimeText
                + "; endTimeText: " + endTimeText
                + "; cardNumText: " + cardNumText
                + "; isCopy: " + isCopy);

        int ret = testViewService.writeCard(roomNumText, starTimeText + endTimeText, isCopy);
        showRespond(ret);
    }

    /**
     * 6.读卡
     *
     * @param event
     */
    @FXML
    public void readCard(ActionEvent event) {
        System.out.println("读卡 ...");
        int ret = testViewService.readCard();
        cardNum.setText("12345678");
        showRespond(ret);
    }

    /**
     * 7.销卡
     *
     * @param event
     */
    @FXML
    public void destroyCard(ActionEvent event) {
        System.out.println("销卡 ...");

        String cardNumText = cardNum.getText();
        try {
            long cardNumLong = Long.parseLong(cardNumText);
            int ret = testViewService.destroyCard(cardNumLong);
            showRespond(ret);
        } catch (NumberFormatException e) {
            System.out.println("无法将字符串转换为长整型数字: " + e.getMessage());
            // 创建一个 Alert 对象
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("错误提示");
            alert.setHeaderText(null); // 不给 null 会有默认信息
            alert.setContentText("卡号必须为存数字");
            // 显示提示框
            alert.showAndWait();
        }
    }

    /**
     * 显示响应信息
     *
     * @param ret
     */
    private void showRespond(int ret) {
        AidierStatus aidierStatus = AidierStatus.fromKey(ret);
        // 创建一个 Alert 对象
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("响应信息提示");
        alert.setHeaderText(null); // 不给 null 会有默认信息
        alert.setContentText("状态码: " + ret + "; 说明: " + aidierStatus.getValue());
        // 显示提示框
        alert.showAndWait();
    }

}
